/**
 * Class responsible for establishing a point-to-point connection 
 * with the server by binding a Java Socket object.
 * 
 * @author Faizan Wajid
 */

package ece452.ims.connection;

import java.io.*;
import java.net.*;

public class Relay implements UserOptions{

	public static final String HOST = "192.168.3.121";
	public static final int PORT = 21000;
	private static Socket echoSocket;
	private static PrintWriter out;
	private static BufferedReader in;
	public static String servReply;
	private static final String nl = "\n";
	private static final String bt = "`";
	
	public Relay(String host){
		try{
			//echoSocket = new Socket(HOST,PORT);
			echoSocket = new Socket(host,PORT);//,port);
			in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
			System.out.println("Welcome to IMS Server!");
			out = new PrintWriter(echoSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
		}catch (UnknownHostException e){
			System.err.println("Cannot ping destination host.");
			System.exit(1);
		}catch (IOException e){
			System.err.println("Cound not get I/O for host.");
			System.exit(1);
		}
	}
	
	public class SendResponse extends Thread{
		public void run(){ try{tx();}catch(IOException e){} }
		public synchronized void tx() throws IOException{
			String userInput;
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
			while(!(userInput = stdIn.readLine()).equals("quit")){
				BufferedReader toSend = new BufferedReader(new InputStreamReader(System.in));
				if(userInput.equals("login")){
					System.out.print("Username: ");
					String uname = toSend.readLine().trim();
					System.out.print("Password: ");
					String pwd = toSend.readLine().trim();
					out.println("0`"+uname+"`"+pwd+"\n");
				}else if(userInput.equals("send")){
					System.out.print("Username: ");
					String uname = toSend.readLine().trim();
					System.out.print("Message: ");
					String message = toSend.readLine().trim();
					out.println("2`"+uname+"`"+message);
				}else if(userInput.equals("logout")){
					System.exit(0);
				}else if(userInput.equals("get list")){
					out.println("5\n");
				}else{
					System.out.println("all other");
					out.println(userInput+"\n");
				}
			}
		}
	}
	
	public void createAccount(String u, String p){ out.println("1"+bt+u+bt+p+nl); }
	
	public void login(String u, String p){ out.println("0"+bt+u+bt+p+nl); }
	
	public void sendMessage(String u, String m){ out.println("3"+bt+u+bt+m+nl); }
	
	public void getListOfFriends(){ out.println("5"+nl); }
	
	public void addFriend(String u){ out.println("6"+bt+u+nl); }
	
	public void removeFriend(String u){ out.println("7"+bt+u+nl); }
	
	public void squelchUser(String u){ out.println("15"+bt+u+nl); }
	
	public void unsquelchUser(String u){ out.println("16"+bt+u+nl); }
	
	public static void ping(){ out.println("19"+nl); }
	
	public static void logout(){ System.exit(0); }
	
	public static PrintWriter sout(){ return out; }
	
	public static BufferedReader sin(){ return in; }

	public Socket socket(){ return echoSocket; } 
	
	public static void test(String host, int port) throws IOException{

		Socket echoSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String userInput;
		
		try{
			//echoSocket = new Socket(HOST, PORT);
			echoSocket = new Socket(host,port);
			System.out.println("Welcome to Mike's Server!");
			out = new PrintWriter(echoSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
		}catch (UnknownHostException e){
			System.err.println("Cannot ping destination host.");
			//System.exit(1);
			return;
		}catch (IOException e){
			System.err.println("Cound not get I/O for host.");
			System.exit(1);
			return;
		}
		
		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		while(!(userInput = stdIn.readLine()).equals("quit")){
			BufferedReader toSend = new BufferedReader(new InputStreamReader(System.in));
			if(userInput.equals("login")){
				System.out.print("Username: ");
				String uname = toSend.readLine().trim();
				System.out.print("Password: ");
				String pwd = toSend.readLine().trim();
				out.println("0`"+uname+"`"+pwd+"\n");
			}else if(userInput.equals("send")){
				System.out.print("Username: ");
				String uname = toSend.readLine().trim();
				System.out.print("Message: ");
				String message = toSend.readLine().trim();
				out.println("2`"+uname+"`"+message);
			}else{
				System.out.println("all other");
				out.println(userInput+"\n");
			}
			System.out.println("reply: " + in.readLine());
		}
		System.out.println("Connection closed.");
		out.close();
		in.close();
		stdIn.close();
		echoSocket.close();
	}
	
	public static void main(String[] fargs) throws Exception{
		//Relay.test("192.168.3.121", PORT);
		Relay rl = new Relay("172.31.60.148");
		//rl.init(rl);
		rl.new SendResponse().start();
		//rl.new GetResponse().start();
	}
	
}
